//
//  CWQ_PopView.m
//  sanda
//
//  Created by 陈武琦 on 16/10/21.
//  Copyright © 2016年 陈武琦. All rights reserved.
//


#import "CWQ_PopView.h"


@implementation CWQ_PopCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *) reuseIdentifier {
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.clipsToBounds = YES;
        self.backgroundColor = [UIColor clearColor];
        
        self.titleLabel = [[UILabel alloc] init];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        [self addSubview:_titleLabel];
        
        self.splitters = [[UIImageView alloc] init];
//        _splitters.image = [UIImage imageNamed:@"SGActionSheet.bundle/cell_splitters@2x"];
        _splitters.backgroundColor = HexRGB_WSJ(235, 235, 235);
        _splitters.contentMode = UIViewContentModeTop;
        _splitters.clipsToBounds = YES;
        [self addSubview:_splitters];
    }
    return self;
}

- (void)layoutSubviews {
    
    [super layoutSubviews];
    self.titleLabel.frame = self.bounds;
    self.splitters.frame = CGRectMake(0, 0, self.bounds.size.width, 1.0f);
}

@end




@interface CWQ_PopView()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UIButton *coverView;
@property(nonatomic, strong)UITableView *tableView;
@end

@implementation CWQ_PopView


/** cell的高度 */
static CGFloat const cell_rowHeight = 44;

/** 底部View弹出的时间 */
static CGFloat const animationDuration = 0.25;




- (instancetype)initWithFrame:(CGRect)frame titlesArray:(NSArray *)titleS selectIndex:(NSInteger)index delegate:(id<CWQ_PopViewDelegate>)delegate {
    
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor clearColor];
        _titleS = titleS;
        _selectRow = index;
        self.delegate_CWQ = delegate;
        [self setupSubviews];
    }
    return self;
}

- (void)setupSubviews {
    
    // 遮盖视图
    self.coverView = [[UIButton alloc] init];
    self.coverView.frame = self.bounds;
    self.coverView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.3];
    [self.coverView addTarget:self action:@selector(dismiss) forControlEvents:(UIControlEventTouchUpInside)];
    [self addSubview:self.coverView];
    
    // tableView的高
    CGFloat tableViewHeight = cell_rowHeight * _titleS.count;
    // 创建tableView
    _tableView = [[UITableView alloc] initWithFrame:(CGRectMake(0, 0, self.bounds.size.width, tableViewHeight)) style:UITableViewStylePlain];
//    _tableView.backgroundColor = [UIColor redColor];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.rowHeight = cell_rowHeight;
    
    [self addSubview:_tableView];
    
}

- (void)setSelectRow:(NSInteger)selectRow {

    _selectRow = selectRow;
    [self.tableView reloadData];

}


- (void)setTitleS:(NSArray *)titleS {
    _titleS = titleS;
    CGFloat tableViewHeight = cell_rowHeight * _titleS.count;
    [self.tableView setFrame:CGRectMake(0, 0, self.bounds.size.width, tableViewHeight)];
    [self.tableView reloadData];

}

- (void)showInView:(UIView *)containView {
    if (self.superview != nil) return;
    
    [UIView animateWithDuration:animationDuration animations:^{
        [containView addSubview:self];
        [self.tableView reloadData];
    }];
}

/** 点击按钮以及遮盖部分执行的方法 */
- (void)dismiss {
    [UIView animateWithDuration:animationDuration animations:^{
        [self removeFromSuperview];
    }];
}

#pragma mark - - - TableView代理以及数据源方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.titleS.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    return cell_rowHeight;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *cellID = @"cell";
    CWQ_PopCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[CWQ_PopCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    
    cell.titleLabel.text = self.titleS[indexPath.row];
    if (indexPath.row == _selectRow) {
        cell.titleLabel.textColor = HexRGB_WSJ(252, 102, 33);
    }
    cell.splitters.hidden = indexPath.row == 0;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([self.delegate_CWQ respondsToSelector:@selector(CWQ_PopView:didSelectRowAtIndexPath:title:)]) {
        [self.delegate_CWQ CWQ_PopView:self didSelectRowAtIndexPath:indexPath.row title:self.titleS[indexPath.row]];
    }
    [self dismiss];
}

@end
